using System;
using System.IO;
using System.Text;

namespace JBORPG.Client
{
    public class ErrorLog
    {
        private String m_FilePath;

        public ErrorLog() : this(@"ErrorLog.txt") { }

        public ErrorLog(String filePath)
        {
            this.m_FilePath = filePath;

            if (!File.Exists(m_FilePath))
                File.CreateText(m_FilePath);
        }

        public void Clear()
        {
            using (StreamWriter sw = new StreamWriter(File.Create(m_FilePath)))
                sw.Close();
        }

        public void Append(String err)
        {
            this.Append(err, true);
        }

        public void Append(String err, Boolean showTime)
        {
            StringBuilder sb = new StringBuilder();

            if (showTime)
            {
                String hour = DateTime.Now.Hour.ToString();
                String min = DateTime.Now.Minute.ToString();

                if (hour.Length <= 1)
                    hour = "0" + hour;
                if (min.Length <= 1)
                    min = "0" + min;

                sb.Append(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + " " + hour + ":" + min + " ");
            }

            sb.Append(err);

            using (StreamWriter sw = new StreamWriter(m_FilePath, true, Encoding.UTF8))
            {
                sw.WriteLine(sb.ToString());
                sw.Close();
            }
        }
    }
}
